package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.models.PosEJ;

public class PosEJService {
	private DatabaseHelper DBHelper;
	Context context;
	
	public PosEJService() {

	}

	public PosEJService(Context context) {
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public List<PosEJ> FindAll(){
		List<PosEJ> obj = new ArrayList<PosEJ>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_pos_ej",null);
		
		while(cursor.moveToNext()){
			int ej_id  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosEJ.EJ_ID));
			int ej_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosEJ.EJ_NO));
			String ej_date  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosEJ.EJ_DATE));
			String ej_content = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosEJ.EJ_CONTENT));
			
			obj.add(new PosEJ(ej_id, ej_no, ej_date, ej_content));
		}
		cursor.close();
		return obj;
	}
	
	public void Insert(PosEJ obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO " + DatabaseSchema.PosEJ.SCHEMA_NAME + " VALUES(?,?,?,?,?,?)",
			
				new Object[] { null ,obj.getEj_no(), obj.getEj_date(), obj.getEj_crt_date(), obj.getEj_crt_time(), obj.getEj_content() });
	}
	public void Update(PosEJ obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"UPDATE " + DatabaseSchema.PosEJ.SCHEMA_NAME + " set ej_no = ?,ej_date = ?,ej_content = ej_content + ? where ej_id = ?",
				new Object[] { obj.getEj_no(), obj.getEj_date(), obj.getEj_content(), obj.getEj_id() });
	}
	
	public void DeleteAll() {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PosEJ.SCHEMA_NAME);
	}
	
	public void DeleteByID(int id) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PosEJ.SCHEMA_NAME + " where ej_id = " + id);
	}
	public List<PosEJ> getAllPosEJ() {
		String sql = "select * from t_pos_ej";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		List<PosEJ> Source = new ArrayList<PosEJ>();
		if (cursor == null) {
			return null;
		}
		while (cursor.moveToNext()) {
			PosEJ ej = new PosEJ(cursor.getInt(0), cursor.getInt(1),
					cursor.getString(2), cursor.getString(3),
					cursor.getString(4), cursor.getString(5));
			Source.add(ej);
		}
		cursor.close();
		return Source;
	}
	
	public List<PosEJ> getAllPosEJById(int ej_id) {
		String sql = "select * from t_pos_ej where ej_id = " + ej_id;
		Cursor cursor = DBHelper.rawQuery(sql, null);
		List<PosEJ> Source = new ArrayList<PosEJ>();
		if (cursor == null) {
			return null;
		}
		while (cursor.moveToNext()) {
			PosEJ ej = new PosEJ(cursor.getInt(0), cursor.getInt(1),
					cursor.getString(2), cursor.getString(3),
					cursor.getString(4), cursor.getString(5));
			Source.add(ej);
		}
		cursor.close();
		return Source;
	}

	public List<PosEJ> getPosEJByDate(String date) {
		String sql = "select * from t_pos_ej where ej_crt_date = '" + date + "'";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		List<PosEJ> Source = new ArrayList<PosEJ>();
		if (cursor == null) {
			return null;
		}
		while (cursor.moveToNext()) {
			PosEJ ej = new PosEJ(cursor.getInt(0), cursor.getInt(1),
					cursor.getString(2), cursor.getString(3),
					cursor.getString(4), cursor.getString(5));
			Source.add(ej);
		}
		cursor.close();
		return Source;
	}

	public List<PosEJ> getPosEJByBeginDateToEndDate(String BeginDate,
			String EndDate) {
		String sql = "select * from t_pos_ej where ej_crt_date >= " + BeginDate
				+ " and ej_crt_date <= " + EndDate + " group by ej_crt_date,ej_crt_time order by ej_no desc";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		List<PosEJ> Source = new ArrayList<PosEJ>();
		if (cursor == null) {
			return null;
		}
		while (cursor.moveToNext()) {
			PosEJ ej = new PosEJ(cursor.getInt(0), cursor.getInt(1),
					cursor.getString(2), cursor.getString(3),
					cursor.getString(4), cursor.getString(5));
			Source.add(ej);
		}
		cursor.close();
		return Source;
	}
	
	
}
